/**
 * Created by Administrator on 2016/8/16.
 */
var maxSize;
var currentPage = 0;
var max = $('#all');
var current = $('#current');
var tbody = $('#news_tbody');
var pre = $('#pre');
var next = $('#next');
var orginno_div = $("#orginno_div");
var key_div = $("#key_div");
var msg;
var push_path = "/push/send/all";
var pushmsgid;
var id;
$(function () {
    init();
    eventinit();
});

function init() {
    init_type();
    current.text(currentPage + 1);
    $.ajax({
        type: 'GET',
        url: host + "/video/number",
        async: false,
        success: function (data) {
            maxSize = data.msg;
            max.text(parseInt(maxSize / video_count));
            page = currentPage + 1;
            turnpage(page);
            currentPage++;
            current.text(currentPage);
        },
        error: function (XMLHttpRequest) {
            alert("请求错误 " + XMLHttpRequest.responseText);
        }
    });
}

function eventinit() {

    pre.on("click", function () {
        if (currentPage > 1) {
            page = currentPage - 1;
            turnpage(page);
            currentPage--;
            current.text(currentPage);
        }
    });

    next.on("click", function () {
        if (currentPage < maxSize) {
            page = currentPage + 1;
            turnpage(page);
            currentPage++;
            current.text(currentPage);
        }
    });
}

function turnpage(page) {
    $.ajax({
        type: 'GET',
        url: host + "/video/page",
        data: {count: news_count, page: page},
        async: false,
        success: function (data) {
            changeTable(data);
        },
        error: function (XMLHttpRequest) {
            alert("请求错误 " + XMLHttpRequest.responseText);
        }
    });
}

function changeTable(data) {
    tbody.empty();
    $.each(data.list, function (i, item) {
        var tr = ' <tr>' +
            ' <td >' + item.url + '</td>' +
            '<td>' + item.introduce + '</td>' +
            '<td><a href=" ' + item.filepath + '">'+item.oldname+'</a></td>' +
            '<td>' + item.opetime + '</td>' +
            '<td><button class="btn btn-primary edit"  type="button" ' +
            'class="btn btn-primary btn-lg"  onclick="push_action(' + item.id + ')">推送</button>' +
            '<button class="btn btn-primary edit" onclick="delete_item('+item.id+')">删除</button>'+
            '</td>' +
            ' </tr>';
        tbody.append(tr);
    });
}


$(".push").on('click',function(){
    ajax_push();
});

function push_action(tempid) {
    id = tempid;
    $.ajax({
        type: 'GET',
        url: host + "/video",
        data: {id: id},
        async: false,
        success: function (data) {
            if (data.code == 1) {
                msg =data;
                $('#myModal').modal('show');
                radio_even();
            } else {
                alert(data.msg);
            }
        },
        error: function (XMLHttpRequest) {
            alert("请求错误 " + XMLHttpRequest.responseText);
        }
    });
}


function delete_item(id) {

    $.ajax({
        type: 'DELETE',
        url: host + "/video?id="+id,
        async: false,
        success: function (data) {
            if (data.code == 1) {
                alert(data.msg);
                window.location.reload();
            } else {
                alert(data.msg);
            }
        },
        error: function (XMLHttpRequest) {
            alert("请求错误 " + XMLHttpRequest.responseText);
        }
    });
}

function radio_even() {
    $("input[name='inlineRadioOptions']").on("click", function () {
        var radio_value = $("input[name='inlineRadioOptions']:checked").val();
        orginno_div.hide();
        key_div.hide();
        switch (radio_value) {
            case "1" :
                setpath("/push/send/all");
                break;
            case "2" :
                setpath("/push/send/terminnals");
                break;
            case "3" :
                setpath("/push/send/users");
                break;
            case "4" :
                orginno_div.show();
                setpath("/push/send/origin/all");
                break;
            case "5" :
                orginno_div.show();
                setpath("/push/send/origin/terminnals");
                break;
            case "6" :
                orginno_div.show();
                setpath("/push/send/origin/users");
                break;
            case "7" :
                key_div.show();
                setpath("/push/send/user");
                break;
            case "8" :
                key_div.show();
                setpath("/push/send/terminnal");
                break;
        }

    });

}

function setpath(path) {
    push_path = path;
}


function ajax_push(){
    var type = $('#type').val();
    msg.type = type;
    var msgid = msg.bean.id;
    alert(msgid);
    msg = JSON.stringify(msg);
    $.ajax({
        type: 'POST',
        url: host + push_path,
        data: {msg: msg, orginno:getCategory(), key: $('#key').val(),type:type,msgid:msgid},
        success: function (data) {
            $('#myModal').modal('hide');
            alert(data.msg);
        },
        error: function (XMLHttpRequest) {
            $('#myModal').modal('hide');
            alert("请求错误 " + XMLHttpRequest.responseText);
        }
    });

}


function init_type(){
    var typeObj = $.parseJSON(videoJson);
    var option= "";
    $.each(typeObj,function(i,item){
        var temp = '<option value="'+item.type+'">'+item.value+'</option>';
        option+=temp;
    });
    $('#type').append(option);
}